package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityGroupingBinding
import com.gexiaobao.pigeon.ui.adapter.AppTeamListAdapter
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/6/26 10:07
 * @Describe    : 团体分组
 */
class ActivityGrouping : BaseActivity<OrgDetailViewModel, ActivityGroupingBinding>() {

    private var orgIcon: String = ""
    private var raceName: String = ""
    private var orgName: String = ""
    private var memberId = 0
    private val mAdapter: AppTeamListAdapter by lazy { AppTeamListAdapter(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.Group_grouping)

        val raceId = intent.getIntExtra("raceId", 0).toString()
        memberId = intent.getIntExtra("memberId", 0)
        orgName = intent.getStringExtra("orgName").toString()
        raceName = intent.getStringExtra("raceName").toString()
        orgIcon = intent.getStringExtra("orgIcon").toString()
        mDatabind.includeOrg.tvIncludeOrgName.text = orgName
        mDatabind.includeOrg.tvIncludeOrgRaceName.text = raceName
        ImageLoaderManager.loadCircleImageFall(this, orgIcon, mDatabind.includeOrg.ivOrgIcon, R.mipmap.profilepic_str_1)

        showLoading(getString(R.string.loading))
        mViewModel.getAppTeamList(raceId)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 12f)))

        mDatabind.includeList.swipeRefreshLayout.init {
            showLoading(getString(R.string.loading))
            mViewModel.getAppTeamList(raceId)
        }

        mAdapter.addChildClickViewIds(R.id.tvNowGroup, R.id.tvGroupProcedure)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.tvNowGroup ->//ActivityNowGroup-----------ActivityGroupNow
                    startActivity<ActivityGroupNow>(
                        "orgName" to orgName,
                        "orgIcon" to orgIcon,
                        "raceName" to raceName,
                        "baseId" to mAdapter.data[position].baseId,
                        "projectId" to mAdapter.data[position].projectId,
                        "memberId" to memberId,
                        "endDate" to mAdapter.data[position].endTime,
                        "describe" to mAdapter.data[position].des
                    )

                R.id.tvGroupProcedure -> startActivity<ActivityGroupProcedure>("rule" to mAdapter.data[position].ruleFile,"title" to "团体规程")
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.appTeamListResult.observe(this) {
            if (mDatabind.includeList.swipeRefreshLayout.isRefreshing) {
                mDatabind.includeList.swipeRefreshLayout.isRefreshing = false
            }
            if (it != null && !it.isEmpty()) {
                mAdapter.setList(it.list)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }
}